/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.mail;

import static org.jboss.seam.ScopeType.APPLICATION;
import static org.jboss.seam.annotations.Install.BUILT_IN;

import java.util.Properties;

import javax.mail.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.ejb3.NonSerializableFactory;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

/**
 * Binds a mail session to the jndi tree as java:/Mail.
 * 
 */
@Name("org.jboss.seam.mail.sessionFactory")
@Startup
@Scope(APPLICATION)
@BypassInterceptors
@Install(precedence = BUILT_IN, classDependencies = "com.dumbster.smtp.SimpleSmtpServer")
public class SessionFactory {

	@Logger
	private Log log;

	// TODO make this configurable via components.xml file
	private Properties mailProps = new Properties();

	@Create
	public void create() throws Exception {
		mailProps.put("mail.smtp.host", "localhost");
		mailProps.put("mail.smtp.port", "24");
		mailProps.put("mail.transport.protocol", "smtp");
		mailProps.put("mail.debug", "true");

		Session session = Session.getDefaultInstance(mailProps);

		try {
			NonSerializableFactory.bind(new InitialContext(), "java:/Mail",
					session);
		} catch (NamingException e) {
			// this must be a seam only test environment
			log.error(e.getMessage(), e);
		}
	}
}
